
#!/bin/bash
#准备阶段


allItems=("pack" "build" "all" "help")
showHelp=0
funShowInf(){

echo "第一个参数值:"${allItems[@]}
echo "pack:  only copy ~/ros2_ws/install and running scripts"
echo "build: only build"
echo "all:   build and pack"

echo "第2个参数为启动脚本名最后能标识项目的:如junYi,guokeda,29suo,不指定时打包默认脚本"

echo "第3个参数表示是否打包启动脚本不输入时不打包,输入yes时表示打包脚本"

echo "使用例子如下"
echo "only build example:       ./distribution-nodes-pack.sh build junYi"
echo "only pack example:        ./distribution-nodes-pack.sh pack junYi"
echo "build and pack example:   ./distribution-nodes-pack.sh all junYi"
}

echo "$@" | grep -wq "help" &&  showHelp=1 || showHelp=0
if [ ${showHelp} -eq 1 ]
then
    funShowInf
exit 1
fi

withsh=0
echo "$3" | grep -wq "yes" &&  withsh=1 || withsh=0
if [ $withsh -eq 1 ]
then
    withsh=1
    echo "需要打包脚本"
else
    echo "不需要打包脚本"
fi

proj=$2
echo "proj:$proj"
packageName=./packages-${proj}.txt

funBdRosPack (){
cd ~/ros2_ws
colcon build --packages-select $1
status=$?
if [ ! $status -eq 0 ]
then
  echo "colcon build --packages-select $1 failed"
  exit 1
fi
}

funBdDDS (){
if [ ! -d ~/build/DDS2ROS ]
then
mkdir -p ~/build/DDS2ROS
fi
cd ~/build/DDS2ROS
cp ~/DDS2ROS/nx/DDS2ROS/dds_profiles.xml ./
cmake ~/DDS2ROS/nx/DDS2ROS
make
}


funGetRep (){

if [ ! -d ~/ros2_ws/src ]
then
mkdir -p ~/ros2_ws/src
fi

cd ~/ros2_ws/src
if [ $4 -eq 0 ]
then
cd ~
fi

info="clone $1 branch:$2 from git:$3 "
echo $info
git clone -b $2 $3
status=$?
if [ ! $status -eq 0 ]
then
  echo "$info failed"
  exit 1
fi
#if dds2ros rename it
if [ $isros -eq 0 ]
then
mv ~/distributed-cluster-products ~/DDS2ROS
fi
}


funBuildOne (){
packpath=~/ros2_ws/src/$1
isros=1
if [ "$1" == "DDS2ROS" ]
then
#dds2ros
isros=0
packpath=~/DDS2ROS/nx/DDS2ROS
fi

if [ -d $packpath ]
then


#checkout to branch
cd $packpath
info="checkout to branch:$2"
echo $info
git checkout -b $2 origin/$2
status=$?
if [ ! $status -eq 0 ]
then
  echo "already exit branch:$2 or not exist this branch in origin"
  git checkout $2 
  status=$?
  if [ ! $status -eq 0 ]
  then
    echo "git checkout $2  failed"
    exit 1
  fi
  
fi
#update the rep via pull
info="pull $1"
echo $info
git pull origin $2
status=$?
if [ ! $status -eq 0 ]
then
  echo "$info failed"
  #exit 1
fi

else

funGetRep $1 $2 $3 $isros

fi

if [ $isros -eq 0 ]
then
  funBdDDS
else
  funBdRosPack $1
fi

}

funpackDDS (){
echo "复制DDS2ROS到 ./nodes"
if [ -f "/usr/bin/DDS2ROS" ]
then
 echo "拷贝/usr/bin/DDS2ROS到 ./nodes"
 cp /usr/bin/DDS2ROS ./nodes/
else

    echo "拷贝~/build/DDS2ROS/DDS2ROS到 ./nodes"
    cp ~/build/DDS2ROS/DDS2ROS ./nodes/
    cp ~/build/DDS2ROS/dds_profiles.xml ./nodes/

fi
}

funPackOne (){
  
  isros=1
  if [ "$1" == "DDS2ROS" ]
  then
    isros=0
  elif [ "$1" == "behavior-process" ]
  then
      echo "复制ros2_ws/tree到 ./nodes/tree"
      cp -fr ~/ros2_ws/tree ./nodes
  fi

  if [ $isros -eq 1 ]
  then
    cp -fr ~/ros2_ws/install/$1 ./nodes/install/
  else
    funpackDDS
  fi


}

funBuildAll (){
if [ ! -d ~/ros2_ws/src ]
then
echo "packalist file:$1 not exist"
exit 1
fi
isBuild=$2
isPack=$3
while read lne
do
  line="${lne// /}"
  if [[ "$line" == "#"* ]]
  then
  echo "skip $line"
  else
  arr=(${line//,/ })
  len=${#arr[@]}
 
  echo "${arr[0]} ${arr[1]} ${arr[2]}"
  if [ $isBuild -eq 1 ]
  then
      if [ $len -lt 3 ]
      then
         echo "skip $line"
         continue
       fi
    funBuildOne ${arr[0]} ${arr[1]} ${arr[2]}
  fi
  if [ $isPack -eq 1 ]
  then
    if [ $len -lt 1 ]
    then
        echo "skip $line"
        continue
    fi
    funPackOne ${arr[0]}
  fi
  fi
done < $1

}

#创建目录存放程序
funInsDir (){
rm -fr ./nodes/install
rm -fr ./nodes/tree
if [ ! -d "./nodes/install" ]
then
mkdir -p ./nodes/install
fi
}

#echo "$@" | grep -wq "build" &&  build=1 || build=0
echo "$1"
if [ "$1" == "build" ]
then
funBuildAll $packageName 1 0
exit 1
elif [ "$1" == "all" ]
then
funInsDir
funBuildAll $packageName 1 1
elif [ "$1" == "pack" ]
then
echo $packageName
funInsDir
funBuildAll $packageName 0 1
else
echo "unknow arg:$@"
echo "全部可选参数:"${allItems[@]}
exit 1
fi





fun3588 (){

echo "复制ros2_ws/tree-3588s/*到 ./nodes/tree/"
if [ ! -d "./nodes/tree" ]
then
mkdir -p ./nodes/tree/
fi

if [ -d "~/ros2_ws/src/behavior-process/tree-3588s" ]
then
cp ~/ros2_ws/src/behavior-process/tree-3588s/* ./nodes/tree/
fi

}



if [ $withsh -eq 1 ]
then
    echo "负责启动脚本到./nodes"
    cp ./droneyee_fly/realfly_startup_$proj.sh ./nodes/realfly_startup.sh
    echo "拷贝cpu gpu频率脚本"
    cp ./max_gpu_freq.sh ./nodes/max_gpu_freq.sh
    cp ./max_cpu_freq.sh ./nodes/max_cpu_freq.sh
#cp ./droneyee_fly/realfly_noterminal_startup_$proj.sh ./nodes/realfly_noterminal_startup.sh
#cp ./droneyee_fly/hilfly_startup_$proj.sh ./nodes/hilfly_startup.sh
fi



pack3588=0
echo "$@" | grep -wq "3588" &&  pack3588=1 || pack3588=0
if [ ${pack3588} -eq 1 ]
then
fun3588
fi

version=""
echo "input version like 1.1.0.1"
read -p "version: " version

tar -czvf nodes.tar.gz nodes
cat ./distribution-nodes-install.sh nodes.tar.gz > disnodes-"${proj}"-"${version}".run

chmod +x disnodes-"${proj}"-"${version}".run

rm -fr nodes
rm -fr nodes.tar.gz
echo "打包完成"















